package client;

import java.rmi.RemoteException;
import java.util.Calendar;
import java.util.Date;

import org.apache.axis2.AxisFault;

import client2.PaymentWSImplStub;
import client2.PaymentWSImplStub.CreateAccount;
import client2.PaymentWSImplStub.CreateAccountResponse;
import client2.PaymentWSImplStub.QueryAccount;
import client2.PaymentWSImplStub.QueryAccountResponse;
import client2.PaymentWSImplStub.Transfer;
import client2.PaymentWSImplStub.TransferResponse;
import client2.PaymentWSImplTransactionExceptionException;


public class TestPaymentWS {

	/**
	 * @param args
	 * @throws RemoteException 
	 * @throws PaymentWSImplTransactionExceptionException 
	 */
	public static void main(String[] args) throws RemoteException, PaymentWSImplTransactionExceptionException {

		PaymentWSImplStub stub = new PaymentWSImplStub();
		String accountID1 ="accone";
		String accountID2 ="acctwo";	
		QueryAccounts(stub, accountID1, accountID2);
		//CreateAccounts(stub);
		//Transfer(stub, accountID1, accountID2);
		
	}
	
	private static void CreateAccounts(PaymentWSImplStub stub) throws RemoteException {
		String accountID1 ="ac1 " + Calendar.getInstance().getTimeInMillis();
		String accountID2 ="ac2 " + Calendar.getInstance().getTimeInMillis();
				
		CreateAccount createAccount = new CreateAccount();
		createAccount.setAccountID(accountID1);
		createAccount.setAmount(1000);
		CreateAccountResponse responseCreateAccount = stub.createAccount(createAccount);
		System.out.println("Create Account: " + responseCreateAccount.get_return());
		
		createAccount.setAccountID(accountID2);
		createAccount.setAmount(2000);
		responseCreateAccount = stub.createAccount(createAccount);
		System.out.println("Create Account: " + responseCreateAccount.get_return());						  
	}
	
	public static void Transfer(PaymentWSImplStub stub, String accountID1, String accountID2) throws RemoteException{
		QueryAccounts(stub, accountID1, accountID2);
				
		// Call the Second Method of the WebService Payment
		Transfer transfer = new Transfer();
		transfer.setAccountID1(accountID1);
		transfer.setAccountID2(accountID2);
		transfer.setAmount(50);
		TransferResponse transferResponse;
		try {
			transferResponse = stub.transfer(transfer);
			System.out.println("Transfer: " + transferResponse.get_return());
			QueryAccounts(stub, accountID1, accountID2);			
		} catch (RemoteException | PaymentWSImplTransactionExceptionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public static void QueryAccounts(PaymentWSImplStub stub, String accountID1, String accountID2) throws RemoteException{
			
		QueryAccount queryAccount = new QueryAccount();
		
		queryAccount.setAccountID(accountID1);
		QueryAccountResponse responseQueryAccount = stub.queryAccount(queryAccount);
		System.out.println("Query Account: " + responseQueryAccount.get_return());
				
		queryAccount.setAccountID(accountID2);
		responseQueryAccount = stub.queryAccount(queryAccount);
		System.out.println("Query Account: " + responseQueryAccount.get_return());
	}
	
}
